// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.services.blob;

import java.io.IOException;

import lichen.entities.IBlobModel;
import lichen.hibernate.EntityService;

import org.apache.tapestry5.upload.services.UploadedFile;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;

/**
 * @author <a href=mailto:Ghostbb@bjmaxinfo.com>Ghostbb</a>
 * @version $Revision$
 * @since 0.0.3
 */
@Transactional
public class BlobDelegate<T extends IBlobModel> implements IBlobDelegate<T>{
	
	/**
	 * 基础服务类-提供对实体简单的CRUD操作
	 */
	private EntityService _entityService;
	
	/**
	 * 封装的上传文件类
	 */
	private UploadedFile _file;
	
	/**
	 * 是否上传
	 * true:上传文件 false:不上传文件
	 */
	private boolean _uploaded;
	
	/**
	 * 已经持久化的的Blob对象(该对象用于编辑或者删除当前持久化对象使用)
	 */
	private T _oldObj;
	
	/**
	 * 当前操作的Blob实体类
	 */
	private Class<T> _clazz;
	
	public BlobDelegate(UploadedFile file,Class<T> clazz, T oldObj,boolean uploaded, EntityService entityService){
		this._file = file;
		this._clazz = clazz;
		this._oldObj = oldObj;
		this._uploaded = uploaded;
		this._entityService = entityService;
	}

	public void save() {
		this.save(null);
		
	}

	public void save(IBlobAroundSaveCallBack callback) {
		if(_file == null){//如果没有上传文件
			if(_oldObj != null){//如果_oldObj不为空
				if(_uploaded){//删除Blob内容
					_oldObj.setBlobData(null);
					_oldObj.setBlobName(null);
					_oldObj.setContentType(null);
				}
				
				this.doCallBackAction(_oldObj, callback);
			}
		} else{
			
			IBlobModel blob = null;
			
			if(_oldObj == null){
				
				//TODO 使用BeanUtils来生成对象
				try {
					blob = _clazz.newInstance();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			} else{
				blob = _oldObj;
			}
			
			blob.setBlobName(_file.getFileName());//保存上传文件名称
			blob.setContentType(_file.getContentType());//保存contentType
			try {//保存文件内容
				blob.setBlobData(FileCopyUtils.copyToByteArray(_file.getStream()));
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			this.doCallBackAction(blob, callback);
		}
	}
	
	/**
	 * 执行callback方法
	 */
	private void doCallBackAction(IBlobModel blob, IBlobAroundSaveCallBack callback){
		if(callback != null){//执行callback
			callback.doBeforeSaveBlob(blob, _entityService);
		}
		
		_entityService.saveOrUpdate(blob);//保存blob
		
		if(callback != null){//执行callback
			callback.doAfterSaveBlob(blob, _entityService);
		}
	}
}
